package com.glela.firewall.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.glela.common.util.Config;

/**
 * Created by liujie 
 */
public class FirewallConfig {

    private static Logger logger = LoggerFactory.getLogger(FirewallConfig.class);

    private static Configuration config = null;

    private static Set<String> whiteList = new HashSet<String>();

    private static Set<String> blackList = new HashSet<String>();

    public static boolean isPropertiesNull() {
        return config == null;
    }

    public static void loadFirewallConfigFile() throws Exception {
        try {
        	FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class).configure(new Parameters().properties().setFileName(Config.getString("firewall.properties.fire.path")).setListDelimiterHandler(new DefaultListDelimiterHandler(',')));
            config = builder.getConfiguration();
            loadBlackList();
            loadWhiteList();
        } catch (ConfigurationException e) {
            logger.error("加载防火墙配置文件出错", e);
            throw new Exception("加载防火墙配置文件出错");
        }
    }

    public static boolean getFirewallOpen() {
        return config.getBoolean("firewall.open", false);
    }

    public static boolean getFirewallMonitorOpen() {
        return config.getBoolean("firewall.monitor.open", false);
    }

    public static boolean getFirewallPersistOpen() {
        return config.getBoolean("firewall.persist.open", false);
    }

    public static int getFirewallMonitorHour() {
        return config.getInt("firewall.monitor.hour", 24);
    }

    public static int getFirewallDevMaxCount() {
        return config.getInt("firewall.dev.maxcount", 50);
    }

    private static String[] getBlackList() {
        return config.getStringArray("firewall.ip.blacklist");
    }

    private static void loadBlackList() {
        String[] blackIpArray = getBlackList();
        blackList.clear();
        for (String ip : blackIpArray) {
            blackList.add(ip);
        }
    }

    private static String[] getWhiteList() {
        return config.getStringArray("firewall.ip.whitelist");
    }

    public static boolean isBlackIp(String sourceIp) {
        return blackList.contains(sourceIp);
    }

    private static void loadWhiteList() {
        String[] whiteIpArray = getWhiteList();
        whiteList.clear();
        for (String ip : whiteIpArray) {
            whiteList.add(ip);
        }
    }

    public static boolean isWhiteIp(String sourceIp) {
        return whiteList.contains(sourceIp);
    }

    public static String show() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("firewall.open=" + getFirewallOpen());
        buffer.append(",firewall.monitor.open=" + getFirewallMonitorOpen());
        buffer.append(",firewall.persist.open=" + getFirewallPersistOpen());
        buffer.append(",firewall.monitor.hour=" + getFirewallMonitorHour());
        buffer.append(",firewall.dev.maxcount=" + getFirewallDevMaxCount());
        buffer.append(",firewall.ip.blacklist=" + Arrays.toString(getBlackList()));
        buffer.append(",firewall.ip.whitelist=" + Arrays.toString(getWhiteList()));
        return buffer.toString();
    }
}
